package com.hejjon.controller;

import com.hejjon.bean.BasicTypeParam;
import com.hejjon.core.bean.ResponseResult;
import com.hejjon.core.bean.StrListBean;
import com.hejjon.core.bean.TreeNode;
import com.hejjon.core.controller.BaseController;
import com.hejjon.core.domain.entity.BasicType;
import com.hejjon.core.domain.vo.BasicTypeVO;
import com.hejjon.service.IBasicTypeService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 基本类型管理
 * @author cs
 * @since 2025-03-06
 */
@RestController
@RequestMapping("/feature/basicType")
public class BasicTypeController extends BaseController {


    @Resource
    private IBasicTypeService basicTypeService;


    /**
     * 创建基本类型
     */
    @PostMapping("/create")
    public ResponseResult create(@RequestBody BasicType basicType) {

        boolean b = basicTypeService.create(basicType);
        return toResult(b, "基础类型创建成功!");
    }


    /**
     * 更新基本类型
     */
    @PostMapping("/update")
    public ResponseResult update(@RequestBody @Validated BasicType basicType) {
        boolean b = basicTypeService.update(basicType);
        return toResult(b, "类型信息更新成功!");
    }


    @PostMapping("/list")
    public ResponseResult list(@RequestBody BasicType basicType) {

        BasicTypeParam param = new BasicTypeParam(basicType.getName(), basicType.getText());
        List<BasicTypeVO> result = basicTypeService.listByCategoryIdAndParams(basicType.getCategoryId(), param);
        return success(result);
    }

    @PostMapping("/removeByIds")
    public ResponseResult removeByIds(@RequestBody StrListBean req) {
        boolean stat = basicTypeService.removeByIds(req.getIds());
        return toResult(stat, "基础类型删除成功");
    }

    @GetMapping("/getTreeData")
    public ResponseResult getTreeData() {
        List<TreeNode<String>> treeData = basicTypeService.findTreeData();
        return success(treeData);
    }


    /**
     * 查询所有的 存储类型 varchar, int 等
     * @return
     */
    @GetMapping("/listAllJdbcType")
    public ResponseResult listAllJdbcType() {
        return success(basicTypeService.listAllJdbcType());
    }

}

